package visao;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import modelo.Administrador;
import controle.ControleAdministrador;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.awt.Toolkit;
import javax.swing.border.BevelBorder;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import java.awt.ComponentOrientation;
import javax.swing.border.LineBorder;

/**
 * 
 * Essa classe serve para o administrador logar no sistema, para cadastrar
 * produtos e clientes, realizar buscas e alterar dados.
 * 
 * @author Carlos, Derek e Hellen
 */

public class Login extends JFrame {

	private JPanel contentPane;
	private JLabel lblNewLabel_1;
	private JTextField txtLogin;

	private Menu menu = new Menu();
	private ControleAdministrador controleAdministrador;
	private JPasswordField passSenha;
	

	/**
	 * Esse metodo serve para instanciar a classe controleAdministrador.
	 */
	protected void abrirTela() {

		controleAdministrador = new ControleAdministrador();
		

	}

	public static void main(String[] args) {
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.abrirTela();
					frame.setVisible(true);
					

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	

	public Login() {
		
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				"Jar\\Imagens\\apple14.png"));
		setTitle("LOGIN DO SISTEMA");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 745, 517);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);

		final JButton btnEntrar = new JButton("Entrar");
		
		this.getRootPane().setDefaultButton(btnEntrar);

		btnEntrar.addActionListener(new ActionListener() {

			@SuppressWarnings({ "deprecation" })
			public void actionPerformed(ActionEvent arg0) {

				Administrador administrador = new Administrador();

				administrador.setLogin(txtLogin.getText());
				administrador.setSenha(passSenha.getText());
				
				

				try {

					List<Administrador> l = controleAdministrador
							.autenticarAdministrador(administrador);

					if (l != null && (l.isEmpty() == false)) {

						menu.setVisible(true);

						txtLogin.setText(null);
						passSenha.setText(null);

						dispose();

					} else {
						JOptionPane.showMessageDialog(null,
								"Acesso negado! Senha ou login incorreto.");
						txtLogin.setText(null);
						passSenha.setText(null);
					}

				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							"Por favor, digite todos os campos!");
					txtLogin.setText(null);
					passSenha.setText(null);
					
				}

			}

		
			
			
		});
		setFont(new Font("Comic Sans MS", Font.BOLD, 14));
		btnEntrar.setForeground(Color.WHITE);
		btnEntrar.setBackground(Color.BLACK);
		btnEntrar.setBounds(479, 288, 137, 29);
		contentPane.add(btnEntrar);

		lblNewLabel_1 = new JLabel("");
		lblNewLabel_1
				.setIcon(new ImageIcon(
						"Jar\\Imagens\\111161-glowing-green-neon-icon-business-key-hole-sc48.png"));
		lblNewLabel_1.setBounds(-71, 0, 456, 479);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel = new JLabel("Usu\u00E1rio");
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		lblNewLabel.setBounds(446, 126, 208, 37);
		contentPane.add(lblNewLabel);

		JLabel lblSenha = new JLabel("Senha:");
		lblSenha.setForeground(Color.WHITE);
		lblSenha.setHorizontalAlignment(SwingConstants.CENTER);
		lblSenha.setFont(new Font("Comic Sans MS", Font.PLAIN, 15));
		lblSenha.setBounds(446, 205, 208, 37);
		contentPane.add(lblSenha);

		txtLogin = new JTextField();
		txtLogin.setHorizontalAlignment(SwingConstants.CENTER);
		txtLogin.setForeground(Color.WHITE);
		txtLogin.setBackground(Color.BLACK);
		txtLogin.setBounds(415, 164, 264, 23);
		contentPane.add(txtLogin);
		txtLogin.setColumns(10);

		passSenha = new JPasswordField();
		passSenha.setHorizontalAlignment(SwingConstants.CENTER);
		passSenha.setFont(new Font("Comic Sans MS", Font.BOLD, 11));
		passSenha.setForeground(new Color(255, 255, 255));
		passSenha.setBackground(new Color(0, 0, 0));
		passSenha.setBounds(415, 243, 264, 23);
		contentPane.add(passSenha);
		
		JLabel lblDigiteOsDados = new JLabel("Digite os dados");
		lblDigiteOsDados.setHorizontalAlignment(SwingConstants.CENTER);
		lblDigiteOsDados.setForeground(new Color(50, 205, 50));
		lblDigiteOsDados.setFont(new Font("Comic Sans MS", Font.BOLD, 17));
		lblDigiteOsDados.setBounds(395, 86, 306, 29);
		contentPane.add(lblDigiteOsDados);
		
		JPanel panel = new JPanel();
		panel.setBorder(UIManager.getBorder("CheckBox.border"));
		panel.setForeground(new Color(0, 0, 0));
		panel.setBackground(new Color(0, 0, 0));
		panel.setBounds(395, 63, 306, 319);
		contentPane.add(panel);
		
		JLabel lblFloripaCombosDelivery = new JLabel("Floripa Combos Delivery");
		lblFloripaCombosDelivery.setHorizontalAlignment(SwingConstants.CENTER);
		lblFloripaCombosDelivery.setForeground(new Color(255, 255, 255));
		lblFloripaCombosDelivery.setFont(new Font("Comic Sans MS", Font.BOLD, 15));
		lblFloripaCombosDelivery.setBounds(395, 421, 306, 29);
		contentPane.add(lblFloripaCombosDelivery);
	}
}
